package com.superme.filemanager.config;

import lombok.Data;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.superme.common.constant.Constants.DELAY_QUEUE_NAME;
import static com.superme.common.constant.Constants.DELAY_ROUTING_KEY;
import static com.superme.common.constant.Constants.DELAY_EXCHANGE_NAME;

@Configuration
@Data
public class DelayedMqConfig {

    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAY_EXCHANGE_NAME, "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue queue() {
        return new Queue(DELAY_QUEUE_NAME, true);
    }

    @Bean
    public Binding binding(Queue queue, CustomExchange delayExchange) {
        return BindingBuilder.bind(queue).to(delayExchange).with(DELAY_ROUTING_KEY).noargs();
    }

}
